function SpotifyItem()
{
	this.UNKNOWN_POPULARITY = -1;
	this.id = "";
	this.name = "";
	this.href = "";
	this.popularity = this.UNKNOWN_POPULARITY;
}

/////////////class track

Track.prototype = new SpotifyItem;
Track.prototype.constructor = Track;

function Track()
{
	SpotifyItem.call(this)
	this.album = "";
	this.trackNumber = 0;
	this.length = 0.0;
	this.artistName = "";
	this.artistId = "";
	this.idMap = new Array();
}

Track.prototype.addId = function(itemId, val)
{
	array[itemId] = val;
}

////////////class album

Album.prototype = new SpotifyItem;
Album.prototype.constructor = Album;

function Album()
{
	SpotifyItem.call(this)
	this.artistName = "";
	this.artistID = "";
	this.availableTerritories = new Array();
	this.ids = new Array();
	this.released = -1;
	
}

///////////class Artist
Artist.prototype = new SpotifyItem;
Artist.prototype.constructor = Artist;

function Artist()
{
	SpotifyItem.call(this);
}

//////////class Results

function Results()
{
	this.startIndex = 0;
	this.totalResults = 0;
	this.itemsPerPage = 0;
	this.items = new Array();
}



///Spotify class

function Spotify()
{
	this.baseUrl = "http://ws.spotify.com/";
	
}

//callback function must accept the results as a parameter
Spotify.prototype.searhArtist = function (query, page, callback)
{
	var encodedArtist = $.URLEncode(query);
	var obj = this;
	$.ajax({
			url:this.baseUrl + 'search/1/artist.json?page=' + page + '&q=' + encodedArtist,
			success: function(data){
			 	obj.searchArtistCB(data, callback);
		 	}
		 
		 });
	
}

Spotify.prototype.searchArtistCB = function(data, callback)
{
	var resp = JSON.parse(data);
	var totalResults = resp.info.num_results;
	var startIndex = resp.info.offset;
	var itemsPerPage = resp.info.limit;
	var artistList = new Array();
	var artists = resp.artists;
	for (var i in artists)
	{
		var item = artists[i];
		var artist = new Artist();
		artist.id = item.href;
		artist.name = item.name;
		artist.popularity = item.popularity;
		artistList.push(artist);
	}
	var results = new Results();
	results.items = artistList;
	results.totalResults = totalResults;
	results.startIndex = startIndex;
	results.itemsPerPage = itemsPerPage;
	callback(results);
}

Spotify.prototype.searchTrack = function(query, page, callback)
{
	var encodedTrack = $.URLEncode(query);
	var obj = this;
	$.ajax({
			url:this.baseUrl + 'search/1/track.json?page=' + page + '&q=' + encodedTrack,
			success: function(data){
			 	obj.searchTrackCB(data, callback);
		 	}
		 
		 });
}

Spotify.prototype.searchTrackCB = function(data, callback)
{
	var resp = JSON.parse(data);
	var totalResults = resp.info.num_results;
	var startIndex = resp.info.offset;
	var itemsPerPage = resp.info.limit;
	var tracks = resp.tracks;
	var trackList = new Array();
	
	for (var i in tracks)
	{
		//this.album =
		var item = tracks[i];
		var track = new Track();
		track.trackNumber = item.trackNumber;
		track.lenght = item.length;
		track.href = item.href;
		track.name = item.name;
		trackList.push(track);
	}
	var results = new Results();
	results.items = trackList;
	results.totalResults = totalResults;
	results.startIndex = startIndex;
	results.itemsPerPage = itemsPerPage;
	callback(results);
	
/*
 * 

[{"album": 
        {
        "released": "2011", 
        "href": "spotify:album:74fjslPVQH60ns7vRlv3tW", 
        "name": "21' Sampler", 
        "availability": {"territories": "ES SE"}
        },
  "name": "Someone Like You", 
  "popularity": "0.94543", 
  "external-ids": [{"type": "isrc", "id": "GBBKS1000351"}], 
  "length": 285.0, "href": "spotify:track:19RKuCdPh68WknFtjG9BJN", 
  "artists": [{"href": "spotify:artist:4dpARuHxo51G3z768sgnrY", "name": "Adele"}], 
  "track-number": "4"}

*/	
}

Spotify.prototype.searchTrackByAuthorAndTitle = function(author, title, callback)
{
	var query = 'artist:"' + author + '"  track:"' + title + '"';
	this.searchTrack(query, 1, callback);
	
}